<%--
  Created by IntelliJ IDEA.
  User: carol
  Date: 2018/6/16
  Time: 20:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*,java.sql.*,javax.servlet.*,functest.*"%>
<html>
<head>
    <link type="text/css" rel="stylesheet" href="layui/css/layui.css" media="all">
    <script type="text/javascript" src="layui/layui.js"></script>
    <meta charset="UTF-8">
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");
    String action = request.getParameter("action");
    String city_name = request.getParameter("cityname");
    FindAPrint func =new FindAPrint();
    if (action.equals("del")){//delete city
        String city_id = request.getParameter("cityid");
        String driver="com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/hbpw?useUnicode=true&characterEncoding=utf-8&useSSL=false";//地址
        String user="hbpwuser";
        String pass="LFFLXY";
        Class.forName(driver);                   //加载jdbc驱动
        Connection connection  = (Connection) DriverManager.getConnection(url,user,pass);

        Statement statement = (Statement) connection.createStatement();  //连接实例
        String delsql = "delete from cs where CS_DM=" + city_id + ";";
        statement.executeUpdate(delsql);
    }
    else if (city_name == null){//未传入城市则输出添加表单
%>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>添加城市</legend>
        </fieldset>
        <form class="layui-form" method="post" style="margin: 20px" action="changecity.jsp?action=add">
            <div class="layui-form-item">
                <label class="layui-form-label">城市名称</label>
                <div class="layui-input-block">
                    <input type="text" name="cityname" lay-verify="cityname" placeholder="请输入" autocomplete="on" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" style="text-align: center;">
                <button class="layui-btn" lay-submit="">确认提交</button>
            </div>
        </form>
<%
    }
    else{
        String driver="com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/hbpw?useUnicode=true&characterEncoding=utf-8&useSSL=false";//地址
        String user="hbpwuser";
        String pass="LFFLXY";
        Class.forName(driver);                   //加载jdbc驱动
        Connection connection  = (Connection) DriverManager.getConnection(url,user,pass);

        Statement statement2 = (Statement) connection.createStatement();  //连接实例
        String selectsql2 = "select count(*) as checkname from cs where CS_MC ='" + city_name + "';";
        ResultSet rst2 = statement2.executeQuery(selectsql2);
        if (rst2.next()){
            int checkcity = rst2.getInt("checkname");
            if (checkcity > 0){
%>
<blockquote class="layui-elem-quote layui-quote-nm">已有重名城市！</blockquote>
<%
            }
            else {
                Statement state = (Statement) connection.createStatement();  //连接实例
                String insertsql = "insert into cs(CS_MC) values('" + city_name + "');";
                try {
                    state.executeUpdate(insertsql);
%>
<blockquote class="layui-elem-quote layui-quote-nm">添加成功</blockquote>
<%
                }catch (SQLException e){
                    out.println(insertsql);
                    out.println(e);
                }
            }
        }
    }
    %>
</body>
</html>
<script type="text/javascript">
    layui.use(['form','layer'],function (){
        var form = layui.form
            ,layer = layui.layer;
        form.verify({
            cityname: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[a-zA-Z\u4e00-\u9fa5\\s·]+$").test(value)){
                    return '请输个正常的名称';
                }
            }
        });
        form.render();
    })
</script>
